using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.Sugar;
using Roles = Medianamik.Core.Security.Roles;
using Medianamik.Core.Security;
using Medianamik.Globalization;
using System.Text;

namespace Medianamik.UI.Web.ModalWindows
{
    public partial class VisiblePropertiesWindow : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SetVisiblePropertiesForGroupButton.Click += SetVisiblePropertiesForGroupButtonClick;

            string currentGroup = Request["group"] ?? string.Empty;

            if (currentGroup == String.Empty)
            {
                DisplayDenyMessage(String.Concat(
                    (string)GetGlobalResourceObject("Medianamik_Global", "ArgumentNullException"), " ",
                        "group"));
            }
            else
            {
                _currentGroup = Groups.FirstOrDefault(g => g.Name.Equals(currentGroup, StringComparison.OrdinalIgnoreCase));

                if (CurrentGroup == null)
                {
                    DisplayDenyMessage(String.Concat(
                        (string)GetGlobalResourceObject("Medianamik_Global", "InvalidArgumentException"), " ",
                        "group"));
                }
                else
                {
                    if (!Page.IsPostBack)
                    {
                        BindProperties();
                    }
                }
            }
        }

        void SetVisiblePropertiesForGroupButtonClick(object sender, EventArgs e)
        {
            SecurityManager.AddPropertiesForGroup(VisibleProperties.Items
                .Cast<ListItem>().Where(i => i.Selected).Select(i => i.Value).ToArray(),
                CurrentGroup.Name);

            System.Web.UI.ScriptManager.RegisterStartupScript(this, typeof(VisiblePropertiesWindow),
                       "CloseWindow", "closeWindow('VisiblePropertiesWindow');", true);
        }

        protected Group CurrentGroup
        {
            get { return _currentGroup; }
        }
        Group _currentGroup;

        protected static IEnumerable<Group> Groups
        {
            get
            {
                if (_groups == null)
                {
                    _groups = Roles.Provider.GetAllIGroups().ToList();
                    _groups.RemoveAll(g => g.ID.Equals(MedianamikGroups.SuperAdmins));
                }
                return _groups.AsEnumerable();
            }
        }
        static List<Group> _groups;

        private void DisplayDenyMessage(string message)
        {
            scriptBlock.Visible = false;
            mainPlaceHolder.Visible = false;
            denyPlaceHolder.Visible = true;
            denyMessage.Text = message;
        }

        private void BindProperties()
        {
            VisibleProperties.DataSource = NodeTypeManager.GetNodeType(MedianamikTypes.MedianamikProfile)
               .GetAllProperties().OrderBy(p => p.Name).Select(p => new { p.Name });
            VisibleProperties.DataBind();

            var visibleProperties = SecurityManager.GetPropertiesForGroups(new[] { CurrentGroup.Name }, true)
                .OrderBy(p => p).ToList();

            if (visibleProperties.Count != NodeTypeManager.GetNodeType(MedianamikTypes.MedianamikProfile)
                .GetAllPropertiesWithManyToManyRelations().Count)
            {
                foreach (ListItem item in VisibleProperties.Items)
                {
                    item.Selected = visibleProperties.Any(r => r.Equals(item.Value, StringComparison.OrdinalIgnoreCase));
                }
            }
        }
    }
}
